package com.wut.minio.controller;

import com.wut.core.common.vo.BaseResponse;
import com.wut.core.controller.BaseController;
import com.wut.minio.common.vo.MinioRequest;
import com.wut.minio.service.MinioService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequiredArgsConstructor
@RequestMapping("/minio")
@Api(tags = "Minio模块")
public class MinioController extends BaseController {

    private final MinioService minioService;

    @PostMapping("/upload")
    @ApiOperation(value = "上传文件")
    public BaseResponse<String> upload(@RequestParam("bucketName") String bucketName, @RequestParam("file") MultipartFile file) {
        return BaseResponse.SUCCESS("上传成功", minioService.upload(bucketName, file));
    }

    @GetMapping
    @ApiOperation(value = "获取文件路径")
    public BaseResponse<String> getFilePath(@RequestBody MinioRequest minioRequest) {
        return BaseResponse.SUCCESS(minioService.getFilePath(minioRequest.getBucketName(), minioRequest.getFileName()));
    }

    @PostMapping("/download")
    @ApiOperation(value = "下载文件")
    public BaseResponse<Boolean> download(@RequestBody MinioRequest minioRequest) {
        return BaseResponse.SUCCESS("下载成功", minioService.download(minioRequest.getBucketName(), minioRequest.getFileName(), response));
    }

    @DeleteMapping
    @ApiOperation(value = "删除文件")
    public BaseResponse<Boolean> remove(@RequestBody MinioRequest minioRequest) {
        return BaseResponse.SUCCESS(minioService.remove(minioRequest.getBucketName(), minioRequest.getFileName()));
    }
}
